import { Map, LatLng, Point } from 'leaflet';

/**
 * 坐标转换类，经纬度->像素，像素->经纬度
 */
export default class Transform {
    _map: Map;

    constructor(map: Map) {
        this._map = map;
    }

    translate(loc: LatLng) {
        const point = this._map.latLngToLayerPoint(loc);
        return 'translate(' + point.x + ',' + point.y + ')';
    }

    // 转换成原点像素坐标
    project(loc: LatLng): [number, number] {
        const point = this._map.latLngToLayerPoint(loc);
        return [point.x, point.y];
    }

    unproject(point: Point) {
        const latLng = this._map.layerPointToLatLng(point);

        return [latLng.lat, latLng.lng];
    }

    // point是相对于地图容器的坐标时，适合用此方法
    containerPointToLatLng(point: Point) {
        const latLng = this._map.containerPointToLatLng(point);
        return [latLng.lat, latLng.lng];
    }

    latLngToContainerPoint(loc: LatLng) {
        const point = this._map.latLngToContainerPoint(loc);
        return [point.x, point.y];
    }
}
